package com.apex.util;

import com.apifan.common.random.constant.CreditCardType;
import com.apifan.common.random.source.*;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * 生成随机信息:
 *
 */
public class RandomInfo {

    //获取随机网易邮箱
    public static String getEmail(){
        return InternetSource.getInstance().randomEmail(10, "163.com");
    }
    //获取随机4个汉子
    public static String getHz(){
        return OtherSource.getInstance().randomChinese(4);
    }
    //获取随机男士名称
    public static String getSchoolboy(){
        return PersonInfoSource.getInstance().randomMaleChineseName();
    }
    //获取随机女生名称
    public static String getSchoolgirl(){
        return PersonInfoSource.getInstance().randomFemaleChineseName();
    }
    //获取随中文名（性别随机）
    public static String getHName(){
        return PersonInfoSource.getInstance().randomChineseName();

    }
    //获取随英文名称
    public static String getEnglishName(){
        return PersonInfoSource.getInstance().randomEnglishName();

    }
    //获取文字随机头像
    public static void getAvatar() throws IOException {
        //姓名
        String name = PersonInfoSource.getInstance().randomChineseName();
//头像文件保存路径
        String targetPath = "F:\\Desktop\\avatar\\" + name + ".png";

//使用默认的Dialog字体
        PersonInfoSource.getInstance().generateNamePicture(name, targetPath);

//使用自定义的字体
        String font = "/home/user/font/SourceHanSansCN-Normal.ttf";
        PersonInfoSource.getInstance().generateNamePicture(name, targetPath, font);
    }
    //虚拟获取随机身份证号
    public static String getIdCard(int i){

        if (i % 2 != 0){
            //生成1个随机的虚拟身份证号码，地区为广西壮族自治区，男性，年龄为18岁
            return PersonInfoSource.getInstance().randomMaleIdCard(AreaSource.getInstance().randomProvince(), i);
        }else {
            //生成1个随机的虚拟身份证号码，地区为广西壮族自治区，男性，年龄为18岁
            return PersonInfoSource.getInstance().randomFemaleIdCard(AreaSource.getInstance().randomProvince(), i);
        }

    }
    //虚拟获取随机银行卡号
    public static String getB(){

        Random random = new Random();
        int i = random.nextInt(4) + 1;
        switch (i){
            case 1:
                //生成1个随机虚拟VISA信用卡号码
                String name1 = "VISA信用卡,";
                return name1 + PersonInfoSource.getInstance().randomCreditCardNo(CreditCardType.Visa);
            case 2:
                //生成1个随机虚拟MasterCard信用卡号码
                String name2 = "MasterCard信用卡,";
                return name2 + PersonInfoSource.getInstance().randomCreditCardNo(CreditCardType.MasterCard);
            case 3:
                //生成1个随机虚拟American Express信用卡号码
                String name3 = "AmericanExpress信用卡,";
                return name3 + PersonInfoSource.getInstance().randomCreditCardNo(CreditCardType.Amex);
            case 4:
                //生成1个随机虚拟银联信用卡号码
                String name4 = "银联信用卡,";
                return name4 + PersonInfoSource.getInstance().randomCreditCardNo(CreditCardType.UnionPay);
            default:
                //生成1个随机虚拟JCB信用卡号码
                String name5 = "JCB信用卡,";
                return name5 + PersonInfoSource.getInstance().randomChineseName();
        }
    }
    //生成1个随机中国大陆手机号
    public static String getPhone(){
        return PersonInfoSource.getInstance().randomChineseMobile();
    }
    //生成1个随机域名，域名最大长度为16
    public static String getDomainName(){
        return InternetSource.getInstance().randomDomain(16);
    }
    //随机IPv4地址
    public static String getIP(){

        Random random = new Random();
        if (random.nextInt(2) == 2){
            //生成1个随机公网IPv4地址
            return InternetSource.getInstance().randomPublicIpv4();
        }else {
            //生成1个随机私有(内网)IPv4地址
            return InternetSource.getInstance().randomPrivateIpv4();
        }
    }
    //随机密码
    public static String getPwd(){

        Random random = new Random();
        if (random.nextInt(2) == 2){
            //生成1个随机强密码，长度为16，无特殊字符
            return PersonInfoSource.getInstance().randomStrongPassword(16, false);
        }else {
            //生成1个随机强密码，长度为16，有特殊字符
            return PersonInfoSource.getInstance().randomStrongPassword(16, true);
        }
    }
    //随机地址
    public static String getAddress(){
        //随机获取省份
        String prv = AreaSource.getInstance().randomProvince();

        //随机获取城市(省份+城市，以逗号为分隔符)
        String city = AreaSource.getInstance().randomCity(",");

        //随机获取邮编
        String zipCode = AreaSource.getInstance().randomZipCode();

        //生成1个随机中国大陆详细地址
        String addr = AreaSource.getInstance().randomAddress();

        return addr;
    }
    //随机网名
    public static String getScreenName(){
        //生成1个随机英文网络昵称，最大长度为8个字符
        String nickName = PersonInfoSource.getInstance().randomNickName(8);

        //生成1个随机汉字网络昵称，最大长度为8个汉字
        String nickName2 = PersonInfoSource.getInstance().randomChineseNickName(8);

        //基于随机汉字网络昵称生成1个拼音网络昵称，最大长度为4个汉字
        String nickName3 = PersonInfoSource.getInstance().randomNickName(4);

        return nickName2;
    }
    //随机时间
    public static LocalDateTime getDate(){
        LocalDate beginDate = LocalDate.of(2000,1,11);
        LocalDate endDate = LocalDate.of(2023,2,22);
        LocalDateTime time4 = DateTimeSource.getInstance().randomPastTime(LocalDateTime.now(), 1000000000);
        return time4;
    }
    //随机小数
    public static double getNumber(){
        return NumberSource.getInstance().randomDouble(0.01D, 0.51D);
    }
    //根据身份证号获取生日
    public static Date getBirthDayFromIdCard(String idCard) throws ParseException {
        String birthdayStr = idCard.substring(10, 14); // 取出身份证号码中出生日期的月份和日期部分
        int month = Integer.parseInt(birthdayStr.substring(0, 2)); // 取出月份，即生日字符串的前2位
        int day = Integer.parseInt(birthdayStr.substring(2)); // 取出日期，即生日字符串的后2位
        if (month > 12) { // 如果月份大于12，则减去80得到实际月份
            month -= 80;
        }
        SimpleDateFormat ft = new SimpleDateFormat("MM-dd");
        Date date = ft.parse(String.format("%02d-%02d", month, day)); // 格式化日期字符串
        return date;
    }

    //    根据银行卡号获取 所属银行
    public static String getBank(String bankcards){
        return bankcards.substring(0, bankcards.indexOf(","));
    }

    //    获取银行卡号
    public static String getBankcards(String bankcards){

        return bankcards.substring(bankcards.indexOf(",") + 1);
    }


    public static void main(String[] args) throws ParseException {

        System.out.println("邮箱：" + getEmail());
        System.out.println("汉子4：" + getHz());
        System.out.println("男士：" + getSchoolboy());
        System.out.println("女士：" + getSchoolgirl());
        System.out.println("随机中文名：" + getHName());
        System.out.println("英文名：" + getEnglishName());
        System.out.println("手机号：" + getPhone());
        System.out.println("域名：" + getDomainName());
        System.out.println("IP：" + getIP());
        System.out.println("密码：" + getPwd());
        System.out.println("地址：" + getAddress());
        System.out.println("网名：" + getScreenName());
        System.out.println("时间：" + getDate());
        System.out.println("小数：" + getNumber());

        String idCard = getIdCard(21);
        System.out.println("身份证：" + idCard);
        System.out.println("根据身份证号获取生日:" + getBirthDayFromIdCard(idCard));
        String bankcards = getB();
        System.out.println("银行卡：" + getBankcards(bankcards));
        System.out.println("根据银行卡号获取所属银行:" + getBank(bankcards));

    }
}